﻿// Decompiled with JetBrains decompiler
// Type: Netick.InternalReplicator
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

#nullable disable
namespace Netick;

internal sealed class InternalReplicator
{
  internal NetickEngine Engine;
  internal Entity Entity;
  internal EntityMetaReplicator EntityMetaReplicator;
  internal PlayersReplicator PlayersReplicator;
  internal MismatchChecker MismatchChecker;
  private SimpleAllocator _allocator;

  public unsafe InternalReplicator(
    NetickEngine engine,
    EntityMetaReplicator entityMetaReplicator,
    PlayersReplicator playersReplicator,
    MismatchChecker mismatchChecker)
  {
    this.Engine = engine;
    this._allocator = new SimpleAllocator();
    this.EntityMetaReplicator = entityMetaReplicator;
    this.PlayersReplicator = playersReplicator;
    this.MismatchChecker = mismatchChecker;
    this.MismatchChecker.Engine = engine;
    int stateSizeWord = this.EntityMetaReplicator.GetSizeWords() + this.PlayersReplicator.GetSizeWords() + this.MismatchChecker.GetSizeWords();
    this.Entity = new Entity(engine, (INetickEntity) new DummyEntity(), (ISafeAllocator) this._allocator, (IAllocator) this._allocator, (long) stateSizeWord, true, false, true);
    this.Entity.RawId = 500000;
    int* statePtr = this.Entity.StatePtr;
    int* stateStart1 = statePtr + this.EntityMetaReplicator.GetSizeWords();
    int* stateStart2 = stateStart1 + this.PlayersReplicator.GetSizeWords();
    this.EntityMetaReplicator.Init(this.Entity, statePtr);
    this.PlayersReplicator.Init(this.Entity, stateStart1);
    this.MismatchChecker.Init(this.Entity, stateStart2);
    this.EntityMetaReplicator.Reset();
    this.PlayersReplicator.Reset();
    this.MismatchChecker.Reset();
    if (!this.Engine.IsServer)
      return;
    this.Entity.Process();
  }

  public void Reset()
  {
    this.EntityMetaReplicator.Reset();
    this.PlayersReplicator.Reset();
    this.MismatchChecker.Reset();
    if (!this.Engine.IsServer)
      return;
    this.Entity.Process();
  }

  public void PostRead()
  {
    this.MismatchChecker.PostReadPhase1();
    this.PlayersReplicator.PostReadPhase1();
    this.EntityMetaReplicator.PostReadPhase1();
  }
}
